#include "mapstationeditview.h"
#include "ui_mapstationeditview.h"
#include "onemap.h"
#include <QMessageBox>

extern double g_mapRatio;

MapStationEditView::MapStationEditView(OneMap *oneMapIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MapStationEditView)
{
    ui->setupUi(this);

    oneMap=oneMapIn;

    model_stationInfo = new QStandardItemModel();

    connect(ui->tableView_stationInfo,SIGNAL(clicked(QModelIndex)),this,SLOT( showChooseStation_slot() ));

}

MapStationEditView::~MapStationEditView()
{
    delete ui;
}

void MapStationEditView::showView_slot()
{
    showStationInfo();
    show();
}

void MapStationEditView::showStationInfo()
{
    std::list<MapPoint *> tmpList=oneMap->getPoints_sort();

    model_stationInfo->clear();
    QStringList labels6 = QObject::trUtf8("id,名称,物理x,物理y,物理r").simplified().split(",");
    model_stationInfo->setHorizontalHeaderLabels(labels6);
    ui->tableView_stationInfo->setColumnWidth(1,120);
    //定义item
    QStandardItem* item = 0;

    int i=0;
    for (auto p : tmpList)
    {
        item = new QStandardItem(QString::number(p->getId()));
        model_stationInfo->setItem(i,0,item);
        item = new QStandardItem(QString::fromStdString(p->getName()));
        model_stationInfo->setItem(i,1,item);
        item = new QStandardItem(QString::number(p->getRealX(),'f',3));
        model_stationInfo->setItem(i,2,item);
        item = new QStandardItem(QString::number(p->getRealY(),'f',3));
        model_stationInfo->setItem(i,3,item);
        item = new QStandardItem(QString::number(p->getRealA(),'f',3));
        model_stationInfo->setItem(i,4,item);
        i++;

    }

    ui->tableView_stationInfo->setModel(model_stationInfo);
    ui->tableView_stationInfo->resizeColumnToContents(0);
    ui->tableView_stationInfo->resizeColumnToContents(1);
    ui->tableView_stationInfo->resizeColumnToContents(2);
    ui->tableView_stationInfo->show();
}

void MapStationEditView::on_pushButton_setNewStationId_clicked()
{

    int oldstationId=ui->lineEdit_originId->text().toInt();
    int newId=ui->lineEdit_newId->text().toInt();
    QString newName=ui->lineEdit_newName->text();

    int tmpKey=oneMap->modifyPointId(oldstationId, newId,newName.toStdString());
    switch(tmpKey)
    {
    case 1:

        break;
    case -1:
        QMessageBox::information(this,QStringLiteral("提示"),"失败：id号不存在！");
        return ;
        break;
    case -2:
        QMessageBox::information(this,QStringLiteral("提示"),"失败：新id号已经被站点使用,请先修改新id！");
        return ;
        break;
    case -3:
        QMessageBox::information(this,QStringLiteral("提示"),"失败：id被其它元素Block占用！");
        return ;
        break;
    case -4:
        QMessageBox::information(this,QStringLiteral("提示"),"失败：id被其它元素group占用！");
        return ;
        break;
    default:
        QMessageBox::information(this,QStringLiteral("提示"),"失败！");
        return;
    }

//    emit sig_propertyChanged(tmpPoint);
    emit updateMapShow_signal();//会导致其它元素ｉｄ也变了，所以要全部更新．
    showStationInfo();
    QMessageBox::information(this,QStringLiteral("提示"),"修改id成功！");
}

void MapStationEditView::on_pushButton_setNewRealCoordinate_clicked()
{
    int stationId=ui->lineEdit_originId->text().toInt();
    MapPoint * tmpPoint=oneMap->OneMap::getPointById(stationId);
    double realx=ui->lineEdit_newX->text().toDouble();
    double realy=ui->lineEdit_newY->text().toDouble();
    double realA=ui->lineEdit_newR->text().toDouble();
    if(nullptr!=tmpPoint)
    {
        tmpPoint->setRealX(realx);
        tmpPoint->setRealY(realy);
        tmpPoint->setRealA(realA);
        if(E_POSITION_TYPE_VMARK!=tmpPoint->getPoseType())
        {
            double tmpValue=realx/g_mapRatio;
            tmpPoint->setX(tmpValue);//12580todo
            tmpValue=realy/g_mapRatio;
            tmpPoint->setY(tmpValue);//12580todo
        }

//        emit updateMapShow_signal();
        emit sig_propertyChanged(tmpPoint);
        showStationInfo();
        QMessageBox::information(this,QStringLiteral("提示"),"物理坐标修改成功,请保存地图生效");
    }
    else
    {
        QMessageBox::warning(this,QStringLiteral("提示"),"点id不存在");
    }

}

void MapStationEditView::showChooseStation_slot()
{
     int row= ui->tableView_stationInfo->currentIndex().row();
     QAbstractItemModel *model = ui->tableView_stationInfo->model ();
     QModelIndex index = model->index(row,0);//选中行第一列的内容
     QVariant data = model->data(index);
     ui->lineEdit_originId->setText(data.toString());
     ui->lineEdit_newId->setText(data.toString());
     ui->label_currentId->setText(data.toString());

     QModelIndex index_name = model->index(row,1);
     QVariant data_name = model->data(index_name);
     ui->lineEdit_newName->setText(data_name.toString());

     QModelIndex index_x = model->index(row,2);
     QVariant data_x = model->data(index_x);
     ui->lineEdit_newX->setText(data_x.toString());

     QModelIndex index_y = model->index(row,3);
     QVariant data_y = model->data(index_y);
     ui->lineEdit_newY->setText(data_y.toString());

     QModelIndex index_r = model->index(row,4);
     QVariant data_r = model->data(index_r);
     ui->lineEdit_newR->setText(data_r.toString());
}

void MapStationEditView::on_pushButton_calculateRealCoordinate_clicked()
{
    double originX,originY,originR;
//    int row= ui->tableView_stationInfo->currentIndex().row();
//    QAbstractItemModel *model = ui->tableView_stationInfo->model ();


//    QModelIndex index_x = model->index(row,2);
//    QVariant data_x = model->data(index_x);
//    originX=data_x.toDouble();


//    QModelIndex index_y = model->index(row,3);
//    QVariant data_y = model->data(index_y);
//    originY=data_y.toDouble();


//    QModelIndex index_r = model->index(row,4);
//    QVariant data_r = model->data(index_r);
//    originR=data_r.toDouble();

    originX=ui->lineEdit_newX->text().toDouble();
    originY=ui->lineEdit_newY->text().toDouble();
    originR=ui->lineEdit_newR->text().toDouble();

    double newX,newY;
    double transX=ui->lineEdit_moveX->text().toDouble();
    double transY=ui->lineEdit_moveY->text().toDouble();
    newX=originX+cos(originR/180*M_PI)*transX+cos((originR+90)/180*M_PI)*transY;
    newY=originY+sin(originR/180*M_PI)*transX+sin((originR+90)/180*M_PI)*transY;
    ui->lineEdit_newX->setText(QString::number(newX,'f',3));
    ui->lineEdit_newY->setText(QString::number(newY,'f',3));
    qDebug()<<"originX"<<originX<<"originY"<<originY<<"originR"<<originR
              <<"transX"<<transX<<"transY"<<transY<<"newX"<<newX<<"newY"<<newY;
    QMessageBox::information(this,QStringLiteral("提示"),"坐标计算成功,请点击修改坐标生效");

}
